using GameBaseFramework.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;


namespace GameUnityFrameWork.Resource
{
    /// <summary>
    /// 管理本地资源目录
    /// </summary>
    public class ResourceHelper
    {
        /// <summary>
        /// 资源完整路径的前缀
        /// </summary>
        public static string ResourcePathPrefix = "Assets/ArtPacks";

        /// <summary>
        /// AssetBundle存放根目录(相对于Application.streamingAssetsPath)
        /// </summary>
        public static string AssetBundlesFolder = "assetbundles";

        /// <summary>
        /// Asset - AssetBundle映射配置路径
        /// </summary>
        public static string PackConfigPath = "Assets/ArtPacks/PackConfig.txt";

        /// <summary>
        /// 当覆盖安装包时，StreamingAssets要比PresistentData新
        /// 热更时直接删除PresistentData的旧资源的话就不用设置
        /// </summary>
        public static bool IsStreamingAssetsVersionNew = false;

        /// <summary>
        /// 路径连接
        /// </summary>
        /// <param name="path1"></param>
        /// <param name="path2"></param>
        /// <returns></returns>
        public static string PathCombine(string path1, string path2)
        {
            return path1 + "/" + path2;
        }


        /// <summary>
        /// 如果UIViews的根目录没有被创建，创建它，并将根目录赋予_UIViewRoot
        /// </summary>
        public static void CreatePathIfNull(string path, bool dontDestroy = false, Vector3? Position = null, Action<GameObject> action = null)
        {
            string[] pathItem = path.Split("/");
            if (pathItem.Length <= 0) return;
            if (Position == null) Position = new Vector3(0f, 0f, 0f);
            List<string> CurrentPathItem = new();
            for (int i = 0; i < pathItem.Length; i++)
            {
                CurrentPathItem.Add(pathItem[i]);
                var obj = GameObject.Find(CurrentPathItem.MakePath());
                if (obj == null)
                {
                    action.Invoke(CreateEmptyGameObjectAtPath(CurrentPathItem, dontDestroy, Position));
                }
                else
                {
                    UnityEngine.Object.DontDestroyOnLoad(obj);
                    obj.transform.position = Position.Value;
                    if (action != null)
                        action.Invoke(obj);
                }
            }
        }

        /// <summary>
        /// 在Path位置下创建一个空GameObject
        /// </summary>
        /// <param name="path"></param>
        public static GameObject CreateEmptyGameObjectAtPath(List<string> pathItem, bool DontDestroyOnLoad = false, Vector3? Position = null)
        {
            List<string> parentPath = new (pathItem);
            parentPath.RemoveAt(parentPath.Count - 1);
            GameObject EmptyGameObject = new();
            if (!GameObject.Find(parentPath.MakePath()).IsUnityNull())
            {
                Transform ParentObject = GameObject.Find(parentPath.MakePath()).transform;
                EmptyGameObject.transform.parent = ParentObject;
                EmptyGameObject.transform.position = Position.Value;
            }
            EmptyGameObject.name = pathItem.Last();
            if (DontDestroyOnLoad) UnityEngine.Object.DontDestroyOnLoad(EmptyGameObject);
            return EmptyGameObject;
        }

        /// <summary>
        /// 获得资源的完整路径
        /// </summary>
        public static string GetFullResourcePath(string path)
        {
            return Path.GetFileNameWithoutExtension(path);
        }

        /// <summary>
        /// 获得本地资源文件路径
        /// 热更新资源会写入到PresistentData目录下
        /// 本地资源先查询PresistentData目录，没有则返回StreamingAssets目录下路径
        /// </summary>
        public static string GetLocalFilePath(string filePath)
        {
            if (!IsStreamingAssetsVersionNew)
            {
                var path = GetPresistentDataFilePath(filePath);
                if (File.Exists(path))
                {
                    return path;
                }
            }
            return GetStreamingAssetsFilePath(filePath);
        }

        /// <summary>
        /// 获得StreamingAssets下的资源文件路径
        /// </summary>
        public static string GetStreamingAssetsFilePath(string filePath)
        {
            return PathCombine(Application.streamingAssetsPath, filePath);
        }

        /// <summary>
        /// 获得PresistentData下的资源文件路径
        /// </summary>
        public static string GetPresistentDataFilePath(string filePath)
        {
            return PathCombine(Application.persistentDataPath, filePath);
        }

        /// <summary>
        /// 根据资源路径映射对应的AssetBundle包名
        /// </summary>
        /// <returns></returns>
        public static string GetAssetBundleName(string path)
        {
            return path.Replace("/", "_").Replace(".", "_").Replace("+", "_").ToLower();
        }

        /// <summary>
        /// 获取本地AssetBundle路径
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetLocalAssetBundlePath(string assetBundleName)
        {
            var assetBundlesPath = PathCombine(AssetBundlesFolder, assetBundleName);
            return GetLocalFilePath(assetBundlesPath);
        }
    }
}